package com.kaili.common.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 配置文件工具类
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 * @author zhengs
 * @version 1.0.0 2017年8月6日 下午10:20:22
 */
public class PropertiesUtil {
    /**
     * 配置文件对象
     */
	private static volatile Properties props;
	
	/**
	 * 配置文件路径(相对class路径)
	 */
	private static final String ROOT_DIR = "config";


	/**
	 * 根据key得到value的值
	 * 
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		return getProperties().getProperty(key);
	}
	
	/**
	 * 读取config目录下的配置文件
	 *  @return
	 */
	public static Properties getProperties() {
        if (props == null) {
            synchronized (PropertiesUtil.class) {
                if (props == null) {
                    URL url = PropertiesUtil.class.getClassLoader().getResource(PropertiesUtil.ROOT_DIR);
                    if(url.toString().startsWith("jar")){
                    	props = getPropertiesByJar();
                    }else{
                    	props = getPropertiesByDir();
                    }
                }
            }
        }
        
        return props;
    }
	
	/**
	 * 加载jar包中指定目录下的所有配置文件
	 * @return
	 */
    public static Properties getPropertiesByJar(){
    	Properties props = null;
    	Properties prop = null;
    	
        JarFile file = null;
		try {
			URL url = PropertiesUtil.class.getClassLoader().getResource(PropertiesUtil.ROOT_DIR);
			JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();  
			file = jarURLConnection.getJarFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
        Enumeration<JarEntry> entrys = file.entries();
        while(entrys.hasMoreElements()){
            JarEntry jar = entrys.nextElement();
            if(jar.getName().startsWith("config/") && !jar.getName().equals("config/")){
                prop = new Properties();
                InputStream is=PropertiesUtil.class.getClassLoader().getResourceAsStream("/"+jar.getName());    
                try {  
                	prop.load(is);
                 	if(props != null){
                		props.putAll(prop);
                	}else{
                		props = prop;
                	}
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }
            }
        }
        try {
			file.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    	return props;
    }
    
    /**
     * 加载指定目录下的所有配置文件
     * @return
     */
    public static Properties getPropertiesByDir(){
    	Properties props = null;
    	
    	// 1.获取配置文件列表
    	File[] fileArray = getAllProperties(PropertiesUtil.ROOT_DIR);
    	if(null == fileArray){
    		return props;
    	}
    	
    	// 2.读取所有配置文件内容
    	Properties prop = null;
    	try {
        	for(File file : fileArray){
        		prop=PropertiesLoaderUtils.loadAllProperties(PropertiesUtil.ROOT_DIR+File.separator+file.getName());
            	
            	if(props != null){
            		props.putAll(prop);
            	}else{
            		props = prop;
            	}
        	}
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    	
    	return props;
    }
    
	/**
	 * 读取配置文件列表
	 *  @param path
	 *  @return
	 */
    private static File[] getAllProperties(String path) {
        URL url = PropertiesUtil.class.getClassLoader().getResource(path);
        File file = null;
        try {
        	file = new File(url.toURI());
        } catch (URISyntaxException e) {
        	e.printStackTrace();
        }
        
        if (file.isDirectory()) {
            return file.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                	return pathname.getName().endsWith(".properties");
                }
            });
        }
        
		return null;
    }
}